using System;
using System.Text;
using System.Reflection;
using System.Globalization;
using System.ComponentModel;

namespace uShareReal.Objects
{
	
	/// <summary>
	/// User account.
	/// </summary>
	[Serializable()]
	public class Account
		: INotifyPropertyChanged
	{
		
		public event PropertyChangedEventHandler PropertyChanged;
		private void OnPropertyChanged(string name)
		{
			if(PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(name));
				
		}
		
		/// <summary>
		/// User account.
		/// </summary>
		public Account () { }
		
		#region Public properties
		
		private int _State = 0;
		/// <summary>
		/// Connection state
		/// </summary>
		public int State
		{
			get { return _State; }
			set 
			{
				_State = (value >= 0 && value <= 2) ? value : 0; 
				
				OnPropertyChanged("State");
			}
		}
		
		private string _Name = null;
		/// <summary>
		/// Username.
		/// </summary>
		public string Name
		{
			get { return _Name; }
			set
			{
				if(string.IsNullOrEmpty(value))
					_Name = null;
				else
					_Name = value.Trim();
				
				OnPropertyChanged("Name");
			}
		}
		
		private string _Host = null;
		/// <summary>
		/// Hostname.
		/// </summary>
		public string Host
		{
			get { return _Host; }
			set
			{
				if(string.IsNullOrEmpty(value))
					_Host = null;
				else
					_Host = value.Trim();
				
				OnPropertyChanged("Host");
			}
		}
		
		private string _Password = null;
		/// <summary>
		/// Account password.
		/// </summary>
		public string Password
		{
			get
			{
				if(string.IsNullOrEmpty(_Password))
					return null;
				
				return Encoding.Default.GetString(Convert.FromBase64String(_Password));
			}
			set
			{
				if(string.IsNullOrEmpty(value))
					_Password = null;
				else
					_Password = Convert.ToBase64String(Encoding.Default.GetBytes(value));
					
				OnPropertyChanged("Password");
			}
		}
		
		private string _Server = null;
		/// <summary>
		/// Connect server.
		/// </summary>
		public string Server
		{
			get { return _Server; }
			set
			{
				if(string.IsNullOrEmpty(value))
					_Server = null;
				else
					_Server = value.Trim();
					
				OnPropertyChanged("Server");
			}
		}
		
		private int _Port = 5222;
		/// <summary>
		/// Connect port server.
		/// </summary>
		public int Port
		{
			get { return _Port; }
			set
			{
				_Port = (value >= 0 && value <= 65535) ? value : 5222;
				
				OnPropertyChanged("Port");
			}
		}
		
		private bool _UseSSL = false;
		/// <summary>
		/// Old SSL security.
		/// </summary>
		public bool UseSSL
		{
			get { return _UseSSL; }
			set
			{
				_UseSSL = value;
				if(UseSSL && UseTLS)
					UseTLS = false;
				
				if(UseSSL && Port == 5222)
					Port = 5223;
				
				if(!UseSSL && Port == 5223)
					Port = 5222;
				
				OnPropertyChanged("UseSSL");
			}
		}
		
		private bool _UseTLS = false;
		/// <summary>
		/// New SSL security.
		/// </summary>
		public bool UseTLS
		{
			get { return _UseTLS; }
			set
			{
				_UseTLS = value;
				if(UseTLS && UseSSL)
					UseSSL = false;
				
				if(UseTLS && UseCompression)
					UseCompression = false;
					
				OnPropertyChanged("UseTLS");
			}
		}
		
		private bool _UseCompression = false;
		/// <summary>
		/// Use stream compression.
		/// </summary>
		public bool UseCompression
		{
			get { return _UseCompression; }
			set
			{
				_UseCompression = value;
				if(UseCompression && UseTLS)
					UseTLS = false;
					
				OnPropertyChanged("UseCompression");
			}
		}
		
		/// <summary>
		/// Default account priority.
		/// </summary>
		public int Priority
		{
			get { return 99; }
		}
		
		/// <summary>
		/// Default resource name.
		/// </summary>
		public string Resource
		{
			get { return Assembly.GetExecutingAssembly().GetName().Name; }
		}
		
		/// <summary>
		/// Default client language.
		/// </summary>
		public string ClientLanguage
		{
			get { return CultureInfo.CurrentCulture.Name; }
		}
		
		/// <summary>
		/// Default client version.
		/// </summary>
		public string ClientVersion
		{
			get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
		}
		
		private bool _IsVisible = false;
		/// <summary>
		/// Account is visible.
		/// </summary>
		public bool IsVisible
		{
			get { return _IsVisible; }
			set
			{
				_IsVisible = value;
				
				OnPropertyChanged("IsVisible");
			}
		}
		
		private string _Status = "On " + Assembly.GetExecutingAssembly().GetName().Name + " client.";
		/// <summary>
		/// Account status message.
		/// </summary>
		public string Status
		{
			get { return _Status; }
			set
			{
				if(string.IsNullOrEmpty(value))
					_Status = null;
				else
					_Status = value.Trim();
					
				OnPropertyChanged("Status");
			}
		}
		
		private bool _AutoConnect = false;
		/// <summary>
		/// Automatic connection.
		/// </summary>
		public bool AutoConnect
		{
			get { return _AutoConnect; }
			set
			{
				_AutoConnect = value;
				
				OnPropertyChanged("AutoConnect");
			}
		}
		
		#endregion
		
	}
	
}

